
#include "serialport.h"
#include <QByteArray>
#include <QString>
#include <QMutex>
#include <QThread>
#include <QWaitCondition>

static const char blankString[] = QT_TRANSLATE_NOOP("SettingsDialog", "N/A");

QSerialPort *m_serial=new QSerialPort();

QString recvString="";

//QObject::connect(m_serial, SIGNAL(readyRead()), m_serial, SLOT(write("sendStr".toLatin1().data(),strlen("sendStr".toLatin1().data()))));

SerialPort::SerialPort(QObject *parent) : QObject(parent)
{
    //connect(m_serial, &QSerialPort::readyRead, this, &SerialPort::ReadBuf);
}

SerialPort::SerialPort(QString port,QString baudrate,QString databits,QString parity,QString stopbits)
{
   serialConnect(port, baudrate, databits, parity, stopbits);
}

QString PortNameStr() { return m_serial->portName(); }

bool SerialPort::serialConnect(QString port,QString baudrate,QString databits,QString parity,

                               QString stopbits)
{

    m_serial->setPortName(port);

    if(!m_serial->open(QIODevice::ReadWrite))
    {

        printf("Open Error");

        return 0;

    }



    switch (baudrate.toInt()) {

    case 4800:

        m_serial->setBaudRate(QSerialPort::Baud4800);

        break;

    case 9600:

        m_serial->setBaudRate(QSerialPort::Baud9600);

        break;

    case 115200:

        m_serial->setBaudRate(QSerialPort::Baud115200);     //设置波特率为115200

        break;

    default:

        printf("BaudRate Error");

        return 0;

    }



    switch (databits.toInt()) {

    case 6:

        m_serial->setDataBits(QSerialPort::Data6);

        break;

    case 7:

        m_serial->setDataBits(QSerialPort::Data7);

        break;

    case 8:

        m_serial->setDataBits(QSerialPort::Data8);          //设置数据位8

        break;

    default:

        printf("DataBits Error");

        return 0;

    }



    switch (parity.toInt()) {

    case 0:

        m_serial->setParity(QSerialPort::NoParity);         //校验位设置为0

        break;

    case 2:

        m_serial->setParity(QSerialPort::EvenParity);

        break;

    case 3:

        m_serial->setParity(QSerialPort::OddParity);

        break;

    default:

        printf("Parity Error");

        return 0;

    }



    switch (stopbits.toInt()) {

    case 1:

        m_serial->setStopBits(QSerialPort::OneStop);        //停止位设置为1

        break;

    case 2:

        m_serial->setStopBits(QSerialPort::TwoStop);

        break;

    case 3:

        m_serial->setStopBits(QSerialPort::OneAndHalfStop);

        break;

    default:

        printf("StopBits Error");

        return 0;

    }



    m_serial->setFlowControl(QSerialPort::NoFlowControl);//设置为无流控制

    return 1;

}

//QStringList SerialPort::querySerialInfo()
//{
//    QStringList serialinfo;
//    QString description;
//    QString manufacturer;
//    QString serialNumber;
//    const auto infos = QSerialPortInfo::availablePorts();
//    for (const QSerialPortInfo &info : infos) {
//        QStringList list;
//        description = info.description();
//        manufacturer = info.manufacturer();
//        serialNumber = info.serialNumber();
//        list << info.portName()
//             << (!description.isEmpty() ? description : blankString)
//             << (!manufacturer.isEmpty() ? manufacturer : blankString)
//             << (!serialNumber.isEmpty() ? serialNumber : blankString)
//             << info.systemLocation()
//             << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : blankString)
//             << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : blankString);

//            serialinfo.append(info.portName());
//    }

//    return serialinfo;

//}

QString SerialPort::serialRead()
{

    return m_serial->readAll();

}

//QString SerialPort::ReadBuf()
//{
//    QByteArray dataArray;

//    dataArray = m_serial->readAll();//读取数据

//    if(!dataArray.isEmpty())
//    {
//        recvString = dataArray.toHex();//把数据直接转化为16进制的字符串形式

//    }
//    //return m_serial->readAll();

//}
void SerialPort::ReadBuf(const QString &s)
{
    QByteArray dataArray;

    dataArray = m_serial->readAll();//读取数据

    if(!dataArray.isEmpty())
    {
        recvString = dataArray.toHex();//把数据直接转化为16进制的字符串形式

    }
}

bool SerialPort::serialWrite(QString sendStr)

{

    if(m_serial->write(sendStr.toLatin1().data(),strlen(sendStr.toLatin1().data())))

        return 1;

    else

        return 0;

}



SerialPort::~SerialPort()

{

    if(m_serial->isOpen())

    {

        m_serial->clear();

        m_serial->close();

    }

    m_serial->deleteLater();

}


